<!--
 * @Author       : Hejh(3010733382@qq.com)
 * @Version      : V1.0
 * @Date         : 2024-02-20 19:27:43
 * @Description  : 考试添加或者修改面板
-->

<template>
  <div class="bg-lightColor exam-edit-con p-3 text-[13px]">
    <n-grid x-gap="12" :cols="1" class="mb-2">
      <n-gi>
        <n-button
          type="primary"
          size="small"
          class="!text-[13px]"
          @click="backView"
        >
          <div class="mr-2 rotate-90 transform">
            <SvgIcon icon="xiala" color="#fff" size="10px"></SvgIcon>
          </div>
          返回
        </n-button>
      </n-gi>
    </n-grid>
    <n-divider class="!my-2" />
    <n-form
      ref="examFormRef"
      :label-width="isSmall ? 100 : 120"
      label-placement="left"
      :label-align="isSmall ? 'left' : 'right'"
      :model="exam"
      size="small"
      require-mark-placement="right"
      :rules="formRule"
    >
      <n-form-item label="科目名称：" path="name">
        <n-input
          v-model:value="exam.name"
          :allow-input="noSideSpace"
          placeholder="请输入科目名称"
          clearable
          class="!w-[300px]"
          :maxlength="50"
        />
      </n-form-item>
      <n-form-item label="父级科目：" path="pid">
        <ExamSelect
          :defaultExamId="exam.pid"
          :multiple="false"
          @change-value="handlePidChange"
          class="!w-[300px]"
        ></ExamSelect>
      </n-form-item>
      <n-form-item label="价格：" path="price">
        <n-input
          v-model:value="exam.price"
          placeholder="请输入价格："
          :allow-input="noSideSpace"
          clearable
          class="!w-[300px]"
          :maxlength="20"
        />
      </n-form-item>
      <n-form-item label="图片：" path="pic">
        <UploadPanel
          v-if="(isModify && isInit) || !isModify"
          :file-list="previewFileList"
          @file-change="handlePicChange"
        ></UploadPanel>
      </n-form-item>
      <n-form-item label=" " v-permission="['admin']">
        <n-button
          type="primary"
          class="grent-btn w-[120px]"
          @click="handleSave(false)"
        >
          保存
        </n-button>
        <n-button
          v-if="!isModify"
          type="primary"
          class="grent-btn w-[120px] ml-2"
          @click="handleSave(true)"
        >
          保存并新增
        </n-button>
      </n-form-item>
    </n-form>
  </div>
</template>

<script setup lang="ts">
import type { UploadFileInfo } from 'naive-ui'
import type { FormInst } from 'naive-ui'
import type { ExamRes, ExamUpdateReq } from '@/types/exam'
import { noSideSpace } from '@/utils'
import { userMenuShow } from '@/hooks/useMenuShow'
import UploadPanel from '@/views/User/Explorer/components/UploadPanel.vue'
const { isSmall } = userMenuShow()
import { ref } from 'vue'

const props = defineProps<{
  isModify: boolean
  examRes?: ExamRes
}>()

const emits = defineEmits<{
  (e: 'backView'): void
  (e: 'changeExam', type: number, data: ExamUpdateReq, addNew: boolean): void
}>()
const backView = () => {
  emits('backView')
}

const previewFileList = ref<UploadFileInfo[]>([])

const showModal = ref(false)
const previewImageUrl = ref('')

const handlePreview = (file: UploadFileInfo) => {
  const { url } = file
  previewImageUrl.value = url as string
  showModal.value = true
}

const handlePicChange = (fileList: UploadFileInfo[]) => {
  if (fileList.length) {
    exam.pic = fileList[0].url as string
  }
}

const exam = reactive<ExamUpdateReq>({
  name: '',
  price: '',
  pic: '',
  pid: ''
})

const isInit = ref(false)

const initSchedule = () => {
  if (props.isModify) {
    const { name, pid, price, pic, id } = props.examRes as ExamRes
    exam.examId = id
    exam.name = name
    exam.pid = pid
    exam.price = price
    exam.pic = pic
    if (exam.pic) {
      previewFileList.value = [
        {
          id: '1',
          status: 'finished',
          percentage: 100,
          url: exam.pic,
          name: exam.name
        }
      ]
    }
    isInit.value = true
  }
}

onMounted(() => {
  initSchedule()
})

const formRule = {
  name: [
    {
      required: true,
      message: '请输入考试名称',
      trigger: ['blur']
    },
    {
      min: 1,
      max: 50,
      trigger: ['blur'],
      message: '长度在1-50位'
    }
  ],
  pid: [
    {
      required: true,
      message: '请选择父级',
      trigger: ['blur']
    }
  ]
}

const examFormRef = ref<FormInst | null>(null)
const handlePidChange = (value: any) => {
  exam.pid = value
}
const handleSave = (addNew: boolean) => {
  examFormRef.value?.validate((error: any) => {
    if (error) {
      return
    }

    if (props.isModify) {
      emits('changeExam', 1, exam, addNew)
    } else {
      emits('changeExam', 0, exam, addNew)
    }

    if (addNew) {
      previewFileList.value = []
      exam.pic = ''
    }
  })
}
</script>

<style scoped lang="scss"></style>
